<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"mathieulin.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"manual","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Java 多线程知识总结多线程概念引入​		期末到了，要复习的科目很多，花叶对此感到很困惑。虽然花叶复习一门考试只需要半小时，但是要把眼前三十门课都给复习完，这还是要耗费花叶不少睡觉的时间的。 ​		于是，花叶想，有没有一种方法，能够让自己可以复习得更快呢，毕竟刚买了好几款游戏。于是，花叶拔了自己的几根头发，然后变出了好几个花叶，一个花叶复习离散，一个花叶复习java，一个花叶复习数据库。。。。。">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 多线程知识总结">
<meta property="og:url" content="https://mathieulin.gitee.io/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/index.html">
<meta property="og:site_name" content="Mathieu&#39;s blog">
<meta property="og:description" content="Java 多线程知识总结多线程概念引入​		期末到了，要复习的科目很多，花叶对此感到很困惑。虽然花叶复习一门考试只需要半小时，但是要把眼前三十门课都给复习完，这还是要耗费花叶不少睡觉的时间的。 ​		于是，花叶想，有没有一种方法，能够让自己可以复习得更快呢，毕竟刚买了好几款游戏。于是，花叶拔了自己的几根头发，然后变出了好几个花叶，一个花叶复习离散，一个花叶复习java，一个花叶复习数据库。。。。。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/3757dcb0-f418-450e-b279-d9724d82c856.png">
<meta property="og:image" content="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/d82eb568-01d1-4c43-b91a-62e2e256f291.png">
<meta property="og:image" content="https://img1.baidu.com/it/u=3014455385,3131745615&fm=253&fmt=auto&app=138&f=PNG?w=647&h=451">
<meta property="og:image" content="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/f3c75a60-cadc-427e-b5d7-c57c62727417.png">
<meta property="article:published_time" content="2022-06-26T14:59:31.000Z">
<meta property="article:modified_time" content="2022-06-26T16:16:14.871Z">
<meta property="article:author" content="Mathieu Lin">
<meta property="article:tag" content="多线程">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/3757dcb0-f418-450e-b279-d9724d82c856.png">


<link rel="canonical" href="https://mathieulin.gitee.io/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://mathieulin.gitee.io/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/","path":"2022/06/26/Java 多线程知识总结/","title":"Java 多线程知识总结"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java 多线程知识总结 | Mathieu's blog</title>
  




<link rel="dns-prefetch" href="https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app">
  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Mathieu's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Mathieu's blog</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">lzhow的debug路程</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Java-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93"><span class="nav-number">1.</span> <span class="nav-text">Java 多线程知识总结</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%A6%82%E5%BF%B5%E5%BC%95%E5%85%A5"><span class="nav-number">1.0.1.</span> <span class="nav-text">多线程概念引入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">1.0.2.</span> <span class="nav-text">线程的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="nav-number">1.0.3.</span> <span class="nav-text">线程的创建</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%A7%E6%89%BFThread%E7%B1%BB"><span class="nav-number">1.0.3.1.</span> <span class="nav-text">继承Thread类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0Runnable%E6%8E%A5%E5%8F%A3"><span class="nav-number">1.0.3.2.</span> <span class="nav-text">实现Runnable接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0Callable%E6%8E%A5%E5%8F%A3%EF%BC%88%E4%BA%86%E8%A7%A3%EF%BC%89"><span class="nav-number">1.0.3.3.</span> <span class="nav-text">实现Callable接口（了解）</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81%E8%BD%AC%E6%8D%A2"><span class="nav-number">1.0.4.</span> <span class="nav-text">线程的状态转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%A4%9A%E4%B8%AA%E7%8A%B6%E6%80%81%E4%BB%A5%E5%8F%8A%E6%96%B9%E6%B3%95"><span class="nav-number">1.0.5.</span> <span class="nav-text">线程的多个状态以及方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%81%9C%E6%AD%A2%E7%BA%BF%E7%A8%8B"><span class="nav-number">1.0.5.1.</span> <span class="nav-text">停止线程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E4%BC%91%E7%9C%A0"><span class="nav-number">1.0.5.2.</span> <span class="nav-text">线程休眠</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E7%A4%BC%E8%AE%A9"><span class="nav-number">1.0.5.3.</span> <span class="nav-text">线程礼让</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BA%BF%E7%A8%8B%E5%90%88%E5%B9%B6"><span class="nav-number">1.0.5.4.</span> <span class="nav-text">线程合并</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E4%B8%AA%E7%BA%BF%E7%A8%8B%E5%B9%B6%E5%8F%91"><span class="nav-number">1.0.6.</span> <span class="nav-text">多个线程并发</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Mathieu Lin</p>
  <div class="site-description" itemprop="description">这是一个记载了lzhow传奇的一生的博客</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/mathieulin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;mathieulin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:19241032@buaa.edu.cn" title="E-Mail → mailto:19241032@buaa.edu.cn" rel="noopener" target="_blank"><i class="fab fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://121.199.35.110/" title="Todo → http:&#x2F;&#x2F;121.199.35.110&#x2F;" rel="noopener" target="_blank">Todo</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://noietch.cn:88/" title="House → http:&#x2F;&#x2F;noietch.cn:88&#x2F;" rel="noopener" target="_blank">House</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" title="Douzero → https:&#x2F;&#x2F;vkceyugu.cdn.bspapp.com&#x2F;VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93&#x2F;0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" rel="noopener" target="_blank">Douzero</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="rss → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>rss</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://mathieulin.github.io/myblog" title="Another-blog → https:&#x2F;&#x2F;mathieulin.github.io&#x2F;myblog" rel="noopener" target="_blank">Another-blog</a>
      </span>
  </div>



        </div>
      </div>
    </div>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1951671851&auto=1&height=66"></iframe>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mathieulin.gitee.io/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Mathieu Lin">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Mathieu's blog">
      <meta itemprop="description" content="这是一个记载了lzhow传奇的一生的博客">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java 多线程知识总结 | Mathieu's blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java 多线程知识总结
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-06-26 22:59:31" itemprop="dateCreated datePublished" datetime="2022-06-26T22:59:31+08:00">2022-06-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-06-27 00:16:14" itemprop="dateModified" datetime="2022-06-27T00:16:14+08:00">2022-06-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
        </span>
    </span>

  
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">评论数：</span>
  
    <a title="waline" href="/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/#waline" itemprop="discussionUrl">
      <span class="post-comments-count waline-comment-count" data-path="/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Java-多线程知识总结"><a href="#Java-多线程知识总结" class="headerlink" title="Java 多线程知识总结"></a>Java 多线程知识总结</h1><h3 id="多线程概念引入"><a href="#多线程概念引入" class="headerlink" title="多线程概念引入"></a>多线程概念引入</h3><p>​		期末到了，要复习的科目很多，花叶对此感到很困惑。虽然花叶复习一门考试只需要半小时，但是要把眼前三十门课都给复习完，这还是要耗费花叶不少睡觉的时间的。</p>
<p>​		于是，花叶想，有没有一种方法，能够让自己可以复习得更快呢，毕竟刚买了好几款游戏。于是，花叶拔了自己的几根头发，然后变出了好几个花叶，一个花叶复习离散，一个花叶复习java，一个花叶复习数据库。。。。。。花叶发现，分成好几个人复习，效率贼高。看着看着离散，看困了又可以写写java大作业，写累了java又可以玩玩数据库，良性循环！</p>
<p><img src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/3757dcb0-f418-450e-b279-d9724d82c856.png" alt="image-20220626213309918"></p>
<p>​		过了半小时，花叶便复习完了期末所有的内容，打开了它的steam。</p>
<p>​		在java中，我们的进程可以调用函数来执行特定的功能。但是，我们发现，进程在调用了函数之后，需要等待函数执行完毕了才能继续往下进行，即花叶需要在复习完离散之后，才可以开始复习java。那么，我们怎么才能像花叶一样，边复习离散，边复习java呢？这就引入了我们的线程的概念。</p>
<h3 id="线程的使用"><a href="#线程的使用" class="headerlink" title="线程的使用"></a>线程的使用</h3><p>​		我们在看奇奇怪怪的视频的时候，我们不仅能看到动态的图像，我们还能听到悦耳的声音，另外还伴随着准确无误的字幕，以及其他同道中人们精彩的弹幕讨论。那么，其实在视频播放器这个进程里，它是包含了图像，声音，字母，弹幕这些线程的。</p>
<p>​		又比如说，我们在与别人进行微信聊天的时候，也是有许许多多线程的，比如，一个线程在等待你的输入，一个线程在处理你的点击事件等。。。。。。</p>
<p>​		线程的实现其实并不是真正地能够同时实现多个任务，而是由于线程之间的切换非常迅速，因此让人觉得线程是能够同时执行的。比如说，花叶一边复习软工，一边复习数据库，但是一心不能二用，强如花叶也做不到，但是她之所以不是一般人，是因为她可以在两者中迅速切换，复习5ms数据库后又复习5ms的软工，然后又复习数据库。这样，在普通人的视角中，花叶仿佛同时完成了两件事情。</p>
<p><img src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/d82eb568-01d1-4c43-b91a-62e2e256f291.png" alt="无标题"></p>
<p>​		这样可能不太容易理解，那么我们把格局打开，或者说我们从花叶（上帝）的视角来看普通人的生活，所谓天上一分钟，地上十年，勒布朗花叶两天连打了两场UBA，但是在上帝花叶看来，这中间其实就是几毫秒不到的时间，那么上帝花叶也就可以认为，勒布朗花叶同时打了两场比赛了。<img src="https://img1.baidu.com/it/u=3014455385,3131745615&fm=253&fmt=auto&app=138&f=PNG?w=647&h=451" alt="img"></p>
<h3 id="线程的创建"><a href="#线程的创建" class="headerlink" title="线程的创建"></a>线程的创建</h3><p>​		好了，我们在前面知道了线程是什么，那接下来我们就来看看线程应该怎么创建吧 ! 这里，我们来介绍线程的三种创建方式。</p>
<h4 id="继承Thread类"><a href="#继承Thread类" class="headerlink" title="继承Thread类"></a>继承Thread类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Thread1</span> <span class="keyword">extends</span> <span class="title class_">Thread</span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Thread1</span><span class="params">(String name)</span> &#123;</span><br><span class="line">       <span class="built_in">this</span>.name=name;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            System.out.println(name + <span class="string">&quot;运行  :  &quot;</span> + i);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                sleep((<span class="type">int</span>) Math.random() * <span class="number">10</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">       </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">		Thread1 mTh1=<span class="keyword">new</span> <span class="title class_">Thread1</span>(<span class="string">&quot;A&quot;</span>);</span><br><span class="line">		Thread1 mTh2=<span class="keyword">new</span> <span class="title class_">Thread1</span>(<span class="string">&quot;B&quot;</span>);</span><br><span class="line">		mTh1.start();</span><br><span class="line">		mTh2.start();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">A运行  :  0</span><br><span class="line">B运行  :  0</span><br><span class="line">A运行  :  1</span><br><span class="line">B运行  :  1</span><br><span class="line">A运行  :  2</span><br><span class="line">B运行  :  2</span><br><span class="line">A运行  :  3</span><br><span class="line">B运行  :  3</span><br><span class="line">A运行  :  4</span><br><span class="line">B运行  :  4</span><br><span class="line"></span><br><span class="line">进程已结束,退出代码0</span><br></pre></td></tr></table></figure>

<p>​		在这个创建线程的方法中，我们创建了继承Thread类的Thread1类。我们在Main中通过new新建了一个线程，而通过线程的start()方法开启了线程（注意，这里只是将该线程变成了可运行态，具体什么时候运行取决于操作系统）。</p>
<p>​		而当该线程开始运行后，将调用run方法，当run方法运行完毕后，该线程结束。</p>
<p>​		花叶同学闲着无聊，于是多点了几次运行，发现了不太一样的运行结果：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">B运行  :  <span class="number">0</span></span><br><span class="line">A运行  :  <span class="number">0</span></span><br><span class="line">B运行  :  <span class="number">1</span></span><br><span class="line">B运行  :  <span class="number">2</span></span><br><span class="line">A运行  :  <span class="number">1</span></span><br><span class="line">B运行  :  <span class="number">3</span></span><br><span class="line">A运行  :  <span class="number">2</span></span><br><span class="line">B运行  :  <span class="number">4</span></span><br><span class="line">A运行  :  <span class="number">3</span></span><br><span class="line">A运行  :  <span class="number">4</span></span><br></pre></td></tr></table></figure>

<p>​		从程序运行的结果可以发现，多线程程序是乱序执行。因此，只有乱序执行的代码才有必要设计为多线程。<br>Thread.sleep()方法调用目的是不让当前线程独自霸占该进程所获取的CPU资源，以留出一定时间给其他线程执行的机会。<br>​		实际上所有的多线程代码执行顺序都是不确定的，每次执行的结果都是随机的。</p>
<p>​		这里的线程执行顺序问题涉及到了操作系统的线程调度问题啦！当然，这其实也很好理解，花叶在复习的时候，当然可以以一个比较固定的顺序来交替进行复习。但是花叶的性情多变，有时候复习完一门之后，还想再进一步地进行复习，而有时候又因为考的太高分了，一段时间内不想复习这一门，因此花叶的复习的顺序其实也是不确定的。</p>
<h4 id="实现Runnable接口"><a href="#实现Runnable接口" class="headerlink" title="实现Runnable接口"></a>实现Runnable接口</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Thread2</span> <span class="keyword">implements</span> <span class="title class_">Runnable</span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Thread2</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name=name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            System.out.println(name + <span class="string">&quot;运行  :  &quot;</span> + i);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep((<span class="type">int</span>) Math.random() * <span class="number">10</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main2</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Thread2</span>(<span class="string">&quot;C&quot;</span>)).start();</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(<span class="keyword">new</span> <span class="title class_">Thread2</span>(<span class="string">&quot;D&quot;</span>)).start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行结果如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">A运行  :  0</span><br><span class="line">B运行  :  0</span><br><span class="line">A运行  :  1</span><br><span class="line">B运行  :  1</span><br><span class="line">A运行  :  2</span><br><span class="line">B运行  :  2</span><br><span class="line">A运行  :  3</span><br><span class="line">B运行  :  3</span><br><span class="line">A运行  :  4</span><br><span class="line">B运行  :  4</span><br><span class="line"></span><br><span class="line">进程已结束,退出代码0</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>我们发现，其实执行结果和方法一没什么不一样的，也是随机的执行噢！</p>
<h4 id="实现Callable接口（了解）"><a href="#实现Callable接口（了解）" class="headerlink" title="实现Callable接口（了解）"></a>实现Callable接口（了解）</h4><h3 id="线程的状态转换"><a href="#线程的状态转换" class="headerlink" title="线程的状态转换"></a>线程的状态转换</h3><p>​		这里找了一张网上的图（画的太好了，而且自己画也不涨字数，就直接拿过来啦）</p>
<p><img src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/f3c75a60-cadc-427e-b5d7-c57c62727417.png" alt="img"></p>
<p>​		我们看到，除了我们刚开始用到的start方法外，还有很多其他的方法。Thread.sleep()方法我们在上面的例子中也有用到过，而其他的这些步骤我们都将在下面来进行进一步地讲解。</p>
<p>​		花叶同学在复习的时候，由于一心不能二用，因此她同时其实还是只能复习一门课的内容。假如花叶正在火急火燎地复习的时候，手机响起了dingdong一声，她打开手机，发现她的舍友给她发了一道题目（题目名为java专题写作），花叶二话不说，将这项直接加进了自己的<del>have done list</del> to do list。但是她并没有马上去做，因为她知道五百页的离散PPT她需要接近一秒才能彻底看完！那么，她舍友给她发的作业线程此时便处于了等待态。待花叶学累了离散（可能就几毫秒吧），花叶便将离散这一线程放入了等待队列中，然后从队列中挑了一个作业来继续运行 。舍友好不容易等到花叶开始帮他做java专题写作（不要好奇为什么她的舍友是他），花叶却发现他粗心的舍友居然没有告诉她要写些什么。没办法，花叶只能缓缓地向她的舍友打出了一个问号。舍友可能去打球了，花叶不能这么等着舍友，因为一分钟的时间足够花叶复习几十门课了，于是花叶将可怜的舍友的作业丢到了阻塞队列中。舍友本以为花叶能在十毫秒内帮他做完这个作业，开开心心地关掉了游戏，打开了微信，只看到花叶缓缓地打出了一个“？”</p>
<p>​		于是，舍友只能爬上了花叶的床，对着她的耳朵大声疾呼：“我爱多线程！”话音未落，这个线程便从阻塞态变成了等候态。舍友刚从床上爬了下来，花叶便已经将这五千字长文发给了他。</p>
<h3 id="线程的多个状态以及方法"><a href="#线程的多个状态以及方法" class="headerlink" title="线程的多个状态以及方法"></a>线程的多个状态以及方法</h3><h4 id="停止线程"><a href="#停止线程" class="headerlink" title="停止线程"></a>停止线程</h4><p>​		花叶已经完成了数据库的学习，那么怎么才能将数据库从花叶的等待库中停止呢？花叶当然可以在复习完后发出命令，停止这门课的复习，但是这多多少少增加了花叶的工作量，不太方便啊！于是花叶想到，当每门课的ppt看完了最后一页，那么就标志着这门课复习完毕了，就不需要再进行复习了（满分！）</p>
<p>​		在java中，我们可以在线程中设置标志位，当达到条件时，我们将标志位置0，然后直接在线程中执行stop方法，便可以使该线程停止咯！</p>
<h4 id="线程休眠"><a href="#线程休眠" class="headerlink" title="线程休眠"></a>线程休眠</h4><p>​		sleep方法我们在前面已经见过了很多次了，其实的话就是相当于延时操作。但是对象具有锁，使用sleep方法并不会释放锁，因此当其他进程尝试申请锁时会被拒绝。</p>
<h4 id="线程礼让"><a href="#线程礼让" class="headerlink" title="线程礼让"></a>线程礼让</h4><p>​		yield方法，可以让当前执行的线程暂停，但是不阻塞。当前线程进入到就绪队列，由CPU进行再次调度。</p>
<p>​		花叶考试的时候总会礼让一下同学们，她会让其他同学先做题，然后她再做。有时候，其它同学会比她更快地做完一道题，但是其他时候，她都能比其他同学先做完。于是，花叶经常说：“我先睡会儿又如何？”</p>
<h4 id="线程合并"><a href="#线程合并" class="headerlink" title="线程合并"></a>线程合并</h4><p>​		join方法，阻塞其他线程，当该线程执行完毕后，其他线程才能继续执行。</p>
<p>​		花叶想打游戏了，因此她会放下其他所有复习的任务，先打游戏，只有当花叶不想打游戏了，她才会继续复习。</p>
<h3 id="多个线程并发"><a href="#多个线程并发" class="headerlink" title="多个线程并发"></a>多个线程并发</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">class TestThread3 implements Runnable&#123;</span><br><span class="line">    private int ticketNums = 10;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public void run()&#123;</span><br><span class="line">        while(true)&#123;</span><br><span class="line">            if(ticketNums&lt;=0)&#123;</span><br><span class="line">                break;</span><br><span class="line">            &#125;</span><br><span class="line">            try &#123;</span><br><span class="line">                Thread.sleep(200);</span><br><span class="line">            &#125; catch (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(Thread.currentThread().getName()+&quot;--&gt;拿到了&quot;+ticketNums--+&quot;票&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public class Main3 &#123;</span><br><span class="line">    public static void main(String[] args)&#123;</span><br><span class="line">        TestThread3 ticket = new TestThread3();</span><br><span class="line">        new Thread(ticket, &quot;小明&quot;).start();</span><br><span class="line">        new Thread(ticket, &quot;小红&quot;).start();</span><br><span class="line">        new Thread(ticket, &quot;小蓝&quot;).start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>得到输出结果如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">小红--&gt;拿到了10票</span><br><span class="line">小蓝--&gt;拿到了9票</span><br><span class="line">小明--&gt;拿到了8票</span><br><span class="line">小蓝--&gt;拿到了7票</span><br><span class="line">小红--&gt;拿到了5票</span><br><span class="line">小明--&gt;拿到了6票</span><br><span class="line">小明--&gt;拿到了4票</span><br><span class="line">小蓝--&gt;拿到了3票</span><br><span class="line">小红--&gt;拿到了4票</span><br><span class="line">小明--&gt;拿到了2票</span><br><span class="line">小蓝--&gt;拿到了2票</span><br><span class="line">小红--&gt;拿到了2票</span><br><span class="line">小明--&gt;拿到了1票</span><br><span class="line">小蓝--&gt;拿到了-1票</span><br><span class="line">小红--&gt;拿到了0票</span><br></pre></td></tr></table></figure>

<p>​		花叶惊喜地发现，大家一共拿了超过了10张票，甚至出现了负票的怪状。花叶发现，假如这样的话，她有一千块，每次拿一百块，她可以拿到1600块，运气好时甚至可以更多！！！于是，她让室友去银行试了试，银行工作人员直接把室友给轰了出来。</p>
<p>​		线程在并发执行时，容易出现数据紊乱的情况，即多个线程同时对一个数据进行读写操作，会出现一些奇怪的问题。那么，银行是怎么防止这个情况发生的呢？</p>
<p>​		当用户取钱的时候，银行会给这个账号加上保护锁，即这个账号此时没法在其他地方进行取款的操作。只有当用户取完款后，银行再把这个锁给取消，其他地方才能进行取款操作。而线程其实也是一样，当线程要对数据进行读写的时候，我们需要给数据加上一个锁（写锁或读锁）。</p>
<p>​		解决方法也很简单，其实给run添加锁就可以了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.concurrent.locks.Lock;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.locks.ReentrantLock;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">TestThread3</span> <span class="keyword">implements</span> <span class="title class_">Runnable</span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">int</span> <span class="variable">ticketNums</span> <span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">Lock</span> <span class="variable">lock</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ReentrantLock</span>();</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                lock.lock();</span><br><span class="line">                <span class="keyword">if</span> (ticketNums &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                Thread.sleep(<span class="number">200</span>);</span><br><span class="line">                </span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;<span class="keyword">finally</span> &#123;</span><br><span class="line">                lock.unlock();</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">&quot;--&gt;拿到了&quot;</span> + ticketNums-- + <span class="string">&quot;票&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span>&#123;</span><br><span class="line">        <span class="type">TestThread3</span> <span class="variable">ticket</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TestThread3</span>();</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(ticket, <span class="string">&quot;小明&quot;</span>).start();</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(ticket, <span class="string">&quot;小红&quot;</span>).start();</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(ticket, <span class="string">&quot;小蓝&quot;</span>).start();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>结果如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">小明--&gt;拿到了10票</span><br><span class="line">小红--&gt;拿到了9票</span><br><span class="line">小蓝--&gt;拿到了8票</span><br><span class="line">小明--&gt;拿到了7票</span><br><span class="line">小红--&gt;拿到了6票</span><br><span class="line">小蓝--&gt;拿到了5票</span><br><span class="line">小明--&gt;拿到了4票</span><br><span class="line">小红--&gt;拿到了3票</span><br><span class="line">小蓝--&gt;拿到了2票</span><br><span class="line">小明--&gt;拿到了1票</span><br><span class="line"></span><br><span class="line">进程已结束,退出代码0</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>​		当然，当我们给这个属性加锁之后，一个属性还好，假如有多个属性的话，当一个线程等待另一个线程的锁释放，而另一个线程又在等待这个线程的锁释放的时候，就会出现死锁的问题。而死锁的解决我们在操作系统以及数据库的学习中已经学习了不少了，这里就不再加以总结了。</p>
<p>​		PS：花叶的舍友在此感谢花叶的友情出演，虽然花叶可能自己都不知道自己出演了。</p>

    </div>

    
    
    

    <footer class="post-footer"><script src="//sdk.jinrishici.com/v2/browser/jinrishici.js"></script>
<script>
  jinrishici.load((result) => {
    let jrsc = document.getElementById('jrsc');
    const data = result.data;
    let author = data.origin.author;
    let title = '《' + data.origin.title + '》';
    let content = data.content.substr(0, data.content.length - 1);
    let dynasty = data.origin.dynasty.substr(0, data.origin.dynasty.length - 1);
    jrsc.innerText = content + ' @ ' + dynasty + '·' + author + title;
  });
</script>
<div style="text-align: center"><span id="jrsc" >正在加载今日诗词....</span></div>

          <div class="post-tags">
              <a href="/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" rel="tag"># 多线程</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/06/10/2022-06-10-Ubuntu%E9%85%8D%E7%BD%AE/" rel="prev" title="Ubuntu配置">
                  <i class="fa fa-chevron-left"></i> Ubuntu配置
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/07/15/2022-07-15-%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="next" title="深度学习笔记">
                  深度学习笔记 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="waline"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mathieu Lin</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div><script color="0,0,255" opacity="0.5" zIndex="-1" count="99" src="https://cdnjs.sourcegcdn.com//ajax/libs/canvas-nest.js/1.0.1/canvas-nest.js"></script>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="waline" type="application/json">{"lang":"zh-CN","enable":true,"serverURL":"https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app","cssUrl":"https://unpkg.com/@waline/client@v2/dist/waline.css","commentCount":true,"pageview":false,"el":"#waline","comment":true,"libUrl":"//unpkg.com/@waline/client@v2/dist/waline.js","path":"/2022/06/26/Java%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB%93/"}</script>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v2/dist/waline.css">
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.waline.el).then(() =>
    NexT.utils.getScript(CONFIG.waline.libUrl, { condition: window.Waline })
  ).then(() => 
    Waline.init(Object.assign({}, CONFIG.waline,{ el: document.querySelector(CONFIG.waline.el) }))
  );
});
</script>

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
